package br.com.yahoo.mau_mss.tutorjavafxgolfx.controller;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.effect.InnerShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;
import br.com.yahoo.mau_mss.tutorjavafxgolfx.TutorJavaFxGolFx;
import br.com.yahoo.mau_mss.tutorjavafxgolfx.model.Produto;

/**
 * <p>Title: ItemApp</p>
 * <p>Description:  </p>
 * <p>Date: Oct 16, 2013, 12:25:37 PM</p>
 * @author Mauricio Soares da Silva (mauricio.soares)
 */
public class ItemApp extends Application {
  private AnchorPane pane;
  private ImageView imgItem;
  private Label lbPreco, lbDescricao;
  private Button btAddCarrinho;
  private static Stage stage;
  public static Produto produto;
  public static int index;
	private static String[] imagesOnLine = {
			"http://www.sportcenterlopes.com.br/images/250_topper_campo_2009replic.jpg",
			"http://1.bp.blogspot.com/_H8uGs8K8kaY/TLZTXR8nIgI/AAAAAAAAF_0/BvpxdqGF4PE/s1600/luva_umbro.png",
			"http://bimg2.mlstatic.com/camisa-nike-active-importada-manga-longa-esportiva-vermelha_MLB-F-199843960_1391.jpg",
			"http://www.showtenis.com.br/images/_product/979/979112/chuteira-nike-mercurial-glide-3-fg-campo--199fd9.jpg",
			"http://www.katy.com.br/imagens/produtos/original/caneleira-topper-trainning-difusion-13340619502673137.jpg" };
  private static String[] images = {
    "images/250_topper_campo_2009replic.jpg",
    "images/luva_umbro.png",
    "images/camisa-nike-active-importada-manga-longa-esportiva-vermelha_MLB-F-199843960_1391.jpg",
    "images/chuteira-nike-mercurial-glide-3-fg-campo--199fd9.jpg",
    "images/caneleira-topper-trainning-difusion-13340619502673137.jpg" };

  /**
   * Create a new instance of <code>ItemApp</code>.
   */
  public ItemApp() {
  }

  @Override
  public void start(Stage stage) throws Exception {
    initComponents();
		initListeners();
		initTransition();
		Scene scene = new Scene(this.pane);
		stage.setScene(scene);
		stage.setTitle(produto.getProduto());
		stage.show();
		initLayout();
		ItemApp.stage = stage;
    
  }
  
  private void initComponents() {
		this.pane = new AnchorPane();
		this.pane.setPrefSize(600, 400);
		this.pane.setStyle(TutorJavaFxGolFx.STYLE_BACKGROUND);
		this.imgItem = new ImageView(new Image(ItemApp.images[index]));
		this.imgItem.setFitWidth(300);
		this.imgItem.setFitHeight(200);
		this.imgItem.setEffect(new Reflection());
		this.lbPreco = new Label(
				"Preço: ".concat(Double.toString(produto.getPreco())));
		this.lbPreco.setStyle("-fx-text-fill: white;");
		this.lbDescricao = new Label("Descrição: ".concat(produto.getProduto()));
		this.lbDescricao.setStyle("-fx-text-fill: white;");
		this.btAddCarrinho = new Button("Adicionar ao Carrinho");
		InnerShadow is = new InnerShadow();
		is.setColor(Color.BLUE);
		btAddCarrinho.setEffect(is);
		this.pane.getChildren().addAll(this.imgItem, this.lbPreco, this.lbDescricao, this.btAddCarrinho);
	}
  
  private void initListeners() {
		this.btAddCarrinho.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				VitrineApp.carrinho.addProdutos(produto);
				try {
					new CarrinhoApp().start(new Stage());
          //new TesteProgressoApp().start(new Stage());
				} catch (Exception e) {
          Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, e);
				}
			}
		});
	}
  
  private void initTransition() {
		Timeline timeline = new Timeline();
		KeyValue kv = new KeyValue(imgItem.opacityProperty(), 0.0);
		KeyFrame kf = new KeyFrame(Duration.millis(2000), kv);
		timeline.getKeyFrames().add(kf);
		timeline.setCycleCount(Timeline.INDEFINITE);
		timeline.setAutoReverse(true);
		timeline.play();
	}
  
  private void initLayout() {
		this.lbPreco.setLayoutX(350);
		this.lbPreco.setLayoutY(50);
		this.lbDescricao.setLayoutX(350);
		this.lbDescricao.setLayoutY(100);
		this.btAddCarrinho.setLayoutX(400);
		this.btAddCarrinho.setLayoutY(300);
	}
  
  public static Stage getStage() {
    return ItemApp.stage;
  }

}